--[[----------------------------------------------------------------------------
LR2PhotoFrameSection.lua
--------------------------------------------------------------------------------
Copyright 2010 Andriy Doroshchuk
 All Rights Reserved.
------------------------------------------------------------------------------]]

require 'LR2Util_Logger'
require 'LR2Util_IM'

-- Lightroom SDK
local LrView 		= import 'LrView'
local LrColor 		= import 'LrColor'
local LrBinding		= import 'LrBinding'
local LrPhotoInfo	= import 'LrPhotoInfo'

---[[ cleanup old preferences to avoid crash on invalid data
local prefs = import 'LrPrefs'.prefsForPlugin() 
prefs = {}
--]]

--============================================================================--
local LR2PhotoFrameSection = {}
-------------------------------------------------------------------------------
LR2PhotoFrameSection.exportPresetFields = {
	{ key = 'frametype',	default = 'outerblur'},
	
	{ key = 'cfInnerWidth',	default = 5 		},
	{ key = 'cfInnerColor',	default = LrColor('black')},
	{ key = 'cfCenterWidth',default = 2 		},
	{ key = 'cfCenterColor',default = LrColor('white') },
	{ key = 'cfOuterWidth',	default = 20 		},
	{ key = 'cfOuterColor',	default = LrColor('black') 	},
	
	{ key = 'ibWidth',		default = 2 		},
	{ key = 'ibBlurWidth',	default = 20 		},
	{ key = 'ibBlurColor',	default = LrColor('black') 	},
	{ key = 'ibBlurColorLevel',	default = 10	},
	{ key = 'ibBlurAmount',	default = 3	},
	
	{ key = 'obWidth',		default = 2 		},
	{ key = 'obColor',		default = LrColor('white')	},
	{ key = 'obBlurWidth',	default = 20 		},
	{ key = 'obBlurColor',	default = LrColor('black')	},
	{ key = 'obBlurColorLevel',	default = 10	},
	{ key = 'obBlurAmount',	default = 3	},

}
-------------------------------------------------------------------------------
function LR2PhotoFrameSection.CreateColorRow(desc)
	local vf = desc.vf
	local row = {
		spacing = vf:control_spacing(),
		vf:static_text {
			title 		= desc.title,
			alignment 	= 'right',
			width		= 100,
		},
		vf:edit_field {
			width_in_digits = 3,
			min 		= desc.min or 0,
			max 		= desc.max or 100,
			precision 	= 0,
			value 		= LrView.bind(desc.tagN),
		},
		vf:slider {
			min 		= desc.min or 0,
			max 		= desc.max or 100,
			integral 	= true,
			width 		= 200,
			value 		= LrView.bind(desc.tagN),
		},
	}
	
	if desc.tagC then
		table.insert(row, 
			vf:static_text {
				title 		= LOC '$$$/LR2/PhotoFrames/Frame/Color=Color',
				alignment 	= 'right',
			})
		table.insert(row, 
			vf:color_well {
				value 		= LrView.bind(desc.tagC),
			})
	end
	
	return row
end
-------------------------------------------------------------------------------
function LR2PhotoFrameSection.CreateColorControl(vf)
	return {
		visible = LrBinding.keyEquals( 'frametype', 'color' ),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf 		= vf,	
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/InnerBorder=Inner Border',		
			tagN 	= 'cfInnerWidth',	
			tagC 	= 'cfInnerColor',	
		})),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf 		= vf,	
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/CenterBorder=Center Border',	
			tagN 	= 'cfCenterWidth',	
			tagC 	= 'cfCenterColor',	
		})),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf		= vf,	
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/OuterBorder=Outer Border',	
			tagN 	= 'cfOuterWidth',
			tagC 	= 'cfOuterColor',	
		})),
	}                                                                                                                                          
end
-------------------------------------------------------------------------------
function LR2PhotoFrameSection.CreateInnerBlurControl(vf)
	return {
		visible = LrBinding.keyEquals( 'frametype', 'innerblur'),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf		= vf,	
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/InnerBorder=Inner Border',
			tagN 	= 'ibWidth',
			min		= 0,
			max		= 10,
		})),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf		= vf,	
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/BlurWidth=Blur Width',
			tagN 	= 'ibBlurWidth',
			min		= 10,
			max		= 100,
		})),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf		= vf,
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/BlurAmount=Blur Amount',
			tagN 	= 'ibBlurAmount',
			min		= 2,
			max		= 10,
		})),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf		= vf,
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/BlurColorLevel=Blur Color Level',
			tagN 	= 'ibBlurColorLevel',
			tagC 	= 'ibBlurColor',	
		})),
	}
end
-------------------------------------------------------------------------------
function LR2PhotoFrameSection.CreateOuterBlurControl(vf)
	return {
		visible = LrBinding.keyEquals( 'frametype', 'outerblur'),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf		= vf,
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/InnerBorder=Inner Border',
			tagN 	= 'obWidth',
			min		= 0,
			max		= 10,
			tagC 	= 'obColor',	
		})),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf		= vf,
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/BlurWidth=Blur Width',
			tagN 	= 'obBlurWidth',
			min		= 10,
			max		= 100,
		})),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf		= vf,
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/BlurAmount=Blur Amount',
			tagN 	= 'obBlurAmount',
			min		= 2,
			max		= 10,
		})),
		vf:row(LR2PhotoFrameSection.CreateColorRow({
			vf		= vf,
			title 	= LOC '$$$/LR2/PhotoFrames/Frame/BlurColorLevel=Blur Color Level',
			tagN 	= 'obBlurColorLevel',
			tagC 	= 'obBlurColor',	
		})),
	}
end
-------------------------------------------------------------------------------
function LR2PhotoFrameSection.CreatePolaroidControl(vf)
	return {
		visible = LrBinding.keyEquals( 'frametype', 'polaroid'),
	}
end
-------------------------------------------------------------------------------
function LR2PhotoFrameSection.sectionForFilterInDialog( viewFactory, propertyTable )
	local vf = viewFactory
	return {
		title 	= LOC '$$$/LR2/PhotoFrames/Frame/SectionTitle=LR2/PhotoFrames Frame',
		synopsis= LrView.bind {
			key 		= 'frametype', 
			object		= propertyTable,
			transform	= function (value, fromTable)
				if value == 'outerblur' then
					return LOC '$$$/LR2/PhotoFrames/Frame/OuterBlurFrame=Outer Blur Frame'
				elseif value == 'innerblur' then
					return LOC '$$$/LR2/PhotoFrames/Frame/InnerBlurFrame=Inner Blur Frame'
				elseif value == 'polaroid' then
					return LOC '$$$/LR2/PhotoFrames/Frame/PolaroidFrame=Polaroid Frame'
				else
					return LOC '$$$/LR2/PhotoFrames/Frame/ColorFrame=Color Frame'
				end
			end,
		},
		
		vf:row{
			spacing = vf:control_spacing(),
			vf:static_text {
				title 		= LOC '$$$/LR2/PhotoFrames/Frame/FrameType=Frame Type:',
				alignment 	= 'right',
				width 		= 100,
			},
			vf:popup_menu {
				value = LrView.bind 'frametype',
				items = {
					{ title = LOC '$$$/LR2/PhotoFrames/Frame/ColorFrame=Color Frame',			value = 'color' 	},
					{ title = LOC '$$$/LR2/PhotoFrames/Frame/InnerBlurFrame=Inner Blur Frame', 	value = 'innerblur' },
					{ title = LOC '$$$/LR2/PhotoFrames/Frame/OuterBlurFrame=Outer Blur Frame', 	value = 'outerblur' },
					{ title = LOC '$$$/LR2/PhotoFrames/Frame/PolaroidFrame=Polaroid Frame', 	value = 'polaroid'	},
				},
			},
		},
		
		vf:row{	
			spacing = vf:control_spacing(),
			vf:column { 
				spacing = vf:control_spacing(),
				place 	= 'overlapping',
				vf:view(LR2PhotoFrameSection.CreateColorControl(vf)),
				vf:view(LR2PhotoFrameSection.CreateInnerBlurControl(vf)),
				vf:view(LR2PhotoFrameSection.CreateOuterBlurControl(vf)),
				vf:view(LR2PhotoFrameSection.CreatePolaroidControl(vf)),
			},
		},
	
	}
end
-------------------------------------------------------------------------------
function LR2PhotoFrameSection.postProcessRenderedPhotos( functionContext, filterContext )
	local renditionOptions = {
		plugin 				= _PLUGIN,
		renditionsToSatisfy = filterContext.renditionsToSatisfy,
		filterSettings 		= function( renditionToSatisfy, exportSettings )
		end,
	}

	
	local frameType = filterContext.propertyTable.frametype
	local toolPath  = GetTool( filterContext.propertyTable.pathToIM )
	--LOG (toolPath)

	local command = ''
	if frameType == 'outerblur' then
	elseif frameType == 'innerblur' then
		local bcolor = makeColor(filterContext.propertyTable.ibBlurColor)
		local bwidth = makeWidth(filterContext.propertyTable.ibBlurWidth)
		local iwidth = makeWidth(filterContext.propertyTable.ibWidth)
		local bswidth = makeWidth(filterContext.propertyTable.ibBlurWidth-filterContext.propertyTable.ibWidth)
		command = 
			lbracket .. ' +clone -blur 0x'.. filterContext.propertyTable.ibBlurAmount .. 
			' -fill ' .. bcolor .. ' -colorize ' .. filterContext.propertyTable.ibBlurColorLevel .. percent .. ' +level 20' .. percent .. ',100' .. percent .. ' ' .. rbracket .. ' ' ..
			lbracket .. ' +clone -gamma 0 -shave ' .. bwidth .. ' -bordercolor white -border ' .. bwidth .. ' ' .. rbracket ..
			' -composite ' ..
			lbracket .. ' +clone -gamma 0 -shave ' .. bwidth .. ' -bordercolor white -border ' .. iwidth .. ' -bordercolor black -border ' .. bswidth .. ' ' .. rbracket .. 
			' -compose screen -composite '
	elseif frameType == 'polaroid' then
		command = '-bordercolor white  -border 5' .. percent .. ' -bordercolor grey60 -border 1 '
	else
		local makeCmd = function (cmd, color, width)
			return cmd .. ' -bordercolor ' .. makeColor(color) .. ' -border ' .. makeWidth(width)
		end
		command = makeCmd(command, filterContext.propertyTable.cfInnerColor, filterContext.propertyTable.cfInnerWidth)
		command = makeCmd(command, filterContext.propertyTable.cfCenterColor, filterContext.propertyTable.cfCenterWidth)
		command = makeCmd(command, filterContext.propertyTable.cfOuterColor, filterContext.propertyTable.cfOuterWidth)
	end
	
	for sourceRendition, renditionToSatisfy in filterContext:renditions(renditionOptions) do
		local success, pathOrMessage = sourceRendition:waitForRender()
		if success and toolPath then
			local path = sourceRendition.destinationPath
			if frameType == 'outerblur' then
				local fwidth = filterContext.propertyTable.obWidth + filterContext.propertyTable.obBlurWidth
				local bcolor = makeColor(filterContext.propertyTable.obBlurColor)
				local ccolor = makeColor(filterContext.propertyTable.obColor)
				local cwidth = makeWidth(filterContext.propertyTable.obWidth)
				local size 	 = LrPhotoInfo.fileAttributes(path)
				command = 
					lbracket .. ' +clone -blur 0x' .. filterContext.propertyTable.obBlurAmount .. 
					' -fill ' .. bcolor .. ' -colorize ' .. filterContext.propertyTable.obBlurColorLevel .. percent .. ' +level 20' .. percent .. ',100' .. percent ..
					' -set option:distort:viewport ' .. (size.width+2*fwidth) .. 'x' .. (size.height+2*fwidth) .. '-' .. fwidth .. '-' .. fwidth ..  
					' -virtual-pixel edge -distort SRT 0 ' .. rbracket .. ' ' .. 
					lbracket .. ' -clone 0 -bordercolor ' .. ccolor .. ' -border ' .. cwidth .. ' ' .. rbracket .. 
					' -delete 0 -gravity center -compose over -composite ' 
			end
			RunIM(toolPath, path, command, renditionToSatisfy)
		end
	end
end
--------------------------------------------------------------------------------

return LR2PhotoFrameSection

